﻿using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;

/// <summary>
/// 背包里装的是TaskItem
/// 但TaskItem只是起到指针作用，它指向AllTaskItems里面对应的那一个
/// 预设体从ItemManager里面找到，然后存入prefab中
/// </summary>
public class Backpack : MonoBehaviour {
	[System.Serializable]
	public class ItemInformation{
		TaskItem itemScr;
		public int num;
		public GameObject Prefab{get{ return itemScr.gameObject;}}
		public TaskItem ItemScr{get{ return itemScr;}}
		public ItemInformation(TaskItem setTI, int setNum=1){
			itemScr = setTI;
			if(itemScr==null){
				Debug.Log("The TaskItem script is null");
			}
			num = setNum;
		}
	};

	public static Backpack _instance;

	/// <summary>
	/// 由索引获得物品列表中预设体的TaskItem脚本
	/// </summary>
	/// <param name="index">Index.</param>
	public TaskItem this[int index]{
		get{ return itemList [index].ItemScr;}
	}
	/// <summary>
	/// 背包中已拥有的物品种数
	/// </summary>
	/// <value>The count.</value>
	public int Count{
		get{ return itemList.Count;}
	}
   
	/// 背包中拥有的物品列表
	public List<ItemInformation> ItemList { get{ return itemList; } }
	List<ItemInformation> itemList;

	void Awake()
	{
		_instance = this;
		itemList = new List<ItemInformation> ();
	}

	/// <summary>
	/// 判断背包中是否有该物品
	/// </summary>
	/// <returns><c>true</c>有<c>false</c> otherwise.</returns>
	/// <param name="iName">物品名</param>
	public bool isContains(string iName){
		foreach (ItemInformation ii in itemList) {
			if (ii.ItemScr.Name == iName) {
				return true;
			}
		}
		return false;
	}
	/// <summary>
	/// 判断背包中是否有该物品
	/// </summary>
	/// <returns><c>true</c>有<c>false</c> otherwise.</returns>
	/// <param name="taskItem">Task item.</param>
	public bool isContains(TaskItem taskItem){
		return isContains (taskItem.Name);
	}

	/// <summary>
	/// 获取物品在背包中的索引
	/// </summary>
	/// <returns>The index.</returns>
	/// <param name="iName">I name.</param>
	public int getIndex(string iName){
		int i;
		for(i=0; i<itemList.Count; i++){
			if (itemList [i].ItemScr.Name == iName) {
				return i;
			}
		}
		return -1;
	}
	/// 获取物品在背包中的索引
	public int getIndex(TaskItem taskItem){
		return getIndex (taskItem.Name);
	}

	/// <summary>
	/// 从背包列表中获取物品对应的预设体
	/// </summary>
	/// <returns>The prefab.</returns>
	/// <param name="iName">I name.</param>
	public GameObject getPrefab(string iName){
		return itemList [getIndex (iName)].Prefab;
	}
	/// 从背包列表中获取物品对应的预设体
	public GameObject getPrefab(TaskItem taskItem){
		return getPrefab (taskItem.Name);
	}
	
    /// <summary>
	/// 从背包列表中获取预设体上的TaskItem脚本
	/// </summary>
	/// <returns>The task item.</returns>
	/// <param name="iName">I name.</param>
	public TaskItem getTaskItem(string iName){
		foreach (ItemInformation ii in itemList) {
			if (ii.ItemScr.Name == iName) {
				return ii.ItemScr;
			}
		}
		return null;
	}

	/// 从背包列表中获取预设体上的TaskItem脚本
	public TaskItem getTaskItem(TaskItem taskItem){
		return getTaskItem (taskItem.Name);
	}
	
    /// <summary>
	/// 获得物体拥有的数量
	/// </summary>
	/// <returns>The number.</returns>
	/// <param name="iName">Item name.</param>
	public int getNumber(string iName){
		return itemList [getIndex(iName)].num;
	}

	/// <summary>
	/// 添加物品到itemList中
	/// </summary>
	/// <returns>物品添加后在itemList中的索引值</returns>
	/// <param name="itemScr">Item scr.</param>
	public int AddItem(TaskItem itemScr){
		if (itemScr != null) {
			int index = getIndex (itemScr);
			if (isContains (itemScr)) {
				itemList [index].num++;
			} else {
				//将被拾取的物品作为预设体
				ItemInformation ii = new ItemInformation (itemScr);
				itemList.Add (ii);
				foreach (Collider col in itemScr.GetComponentsInChildren<Collider>()) {
					Destroy (col);
				}
				itemScr.transform.position = transform.position;
				itemScr.transform.parent = transform;
				itemScr.gameObject.SetActive (false);
				index = Count - 1;
			}
			return index;
		} else {
			Debug.LogError ("The TaskItem is null");
		}
		return -1;
	}

	/// <summary>
	/// 消耗某物品，只有当背包中含有该物品且该物品数量足够时返回true，否则返回false
	/// </summary>
	/// <returns><c>true</c>, if item was used, <c>false</c> otherwise.</returns>
	/// <param name="iName">目标物品</param>
	/// <param name="num">消耗数量</param>
	public bool useItem(string iName, int num=1){
		if (num <= 0) {
			return false;
		}
		int index = getIndex (iName);
		if (index>=0 && itemList [index].num >= num) {
			itemList [index].num -= num;
			if (itemList [index].num <= 0) {
				itemList.RemoveAt(index);
			}
			return true;
		}
		Debug.LogError ("There is no item named " + iName + " in backpack, or the number of the item less than " + num.ToString () + ".");
		return false;
	}
	/// <summary>
	/// 消耗某物品，只有当背包中含有该物品且该物品数量足够时返回true，否则返回false
	/// </summary>
	/// <returns><c>true</c>, if item was used, <c>false</c> otherwise.</returns>
	/// <param name="iName">目标物品</param>
	/// <param name="num">消耗数量</param>
	public bool useItem(TaskItem ti, int num=1){
		return useItem (ti.Name, num);
	}
	
    /// <summary>
	/// 销毁物体
	/// </summary>
	/// <returns><c>true</c>, if item was destroyed, <c>false</c> otherwise.</returns>
	/// <param name="iName">Item name.</param>
	public bool removeItem(string iName){
		int index = getIndex (iName);
		if (index>=0) {
			itemList.RemoveAt(index);
			return true;
		}
		return false;
	}

	/// <summary>
	/// Removes the item.
	/// </summary>
	/// <returns><c>true</c>, if item was removed, <c>false</c> otherwise.</returns>
	/// <param name="ti">Ti.</param>
	public bool removeItem(TaskItem ti){
		return removeItem (ti.Name);
	}
}
